<template>
  <div>
    <van-picker
      show-toolbar
      :columns="gender"
      :default-index="value"
      @cancel="$emit('close')"
      @confirm="onUserGender"
      @change="onChange"
    />
  </div>
</template>

<script>
import { modifyUserData } from '@/api/user.js'

export default {
  name: 'UserGenderPopup',
  data() {
    return {
      gender: ['男', '女'],
      genderData: 0
    }
  },
  props: {
    value: {
      type: [Number, String],
      required: true
    }
  },
  methods: {
    async onUserGender() {
      this.$toast.loading({
        message: '修改中...', // 提示词
        forbidClick: true,
        duration: 0 // 持续时间 当前一直持续
      })
      try {
        await modifyUserData({
          gender: this.genderData
        })

        this.$emit('input', this.genderData)
        this.$emit('close')

        this.$toast.success('修改成功')
      } catch (error) {
        console.log(error)
        this.$toast.fail('修改性别失败')
      }
    },
    onChange(picker, value, index) {
      this.genderData = index
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.van-nav-bar {
  background-color: #fff;
  .van-nav-bar__title {
    color: black;
  }
  .van-icon-arrow-left {
    color: #1989fa !important;
  }
}
.van-field {
  margin-top: 20px;
}
</style>
